/**
 * com.cnhis.his.customer.service.Bci1Service.java
 */
package com.mbmed.doctor.medadvice.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mbmed.doctor.commons.mapper.ModelVo;
import com.mbmed.doctor.medadvice.bo.Bci1Bo;
import com.mbmed.doctor.medadvice.mapper.Bci1;

/**
 * @file  Bci1Service.java
 * @author 刘武
 * @version 0.1
 * @todo	Bci1服务操作接口
 * Copyright(C), 2013-2014
 *		 Shenzhen Coordinates Software Development Co., Ltd.
 * History
 *   	1. Date: 2014-11-01 06:42:53
 *      	Author: 刘武
 *      	Modification: this file was created
 *   	2. ...
 */
@Path("/bci1")
@Component
public class Bci1Service {
	
	private static Logger logger = Logger.getLogger(Bci1.class);
	
	@Autowired
	private Bci1Bo bci1Bo;
	
	@Context
	Request request;

	
	/**
	 * 查找所有数据库记录
     * @return type : List 返回查询操作所有符合条件的记录的VO对象集合，操作失败返回null
     */
	@GET
	@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
    public List<Bci1> getAll() throws Exception{
        List<Bci1> list = new ArrayList<Bci1>();
        return bci1Bo.getAll();        
    }
	
    /**
     * 查找符合条件的所有数据库记录
     * @param id 与数据库中主键对应的值
     * @return type :  返回查询操作所有符合条件的记录的VO对象，操作失败返回null
     * @throws Exception
     */
	@GET
	@Path("{id}")
	@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
    public Bci1 getBci1(@PathParam("id") String id) throws Exception{
    	Bci1 bci1 = null;
    	return bci1Bo.get(id);        
    }
	
	/**
     * 根据不同分类检索数据
     * @param id 与数据库中主键对应的值
     * @return type :  返回查询操作所有符合条件的记录的VO对象，操作失败返回null
     * @throws Exception
     */
	@GET
	@Path("/getadivquerybody")
	@Produces({MediaType.APPLICATION_JSON})
    public ModelVo getAdivQueryBody(
    		@QueryParam("bbx01") java.lang.String bbx01,
    		@QueryParam("bbx13") java.lang.String bbx13
    		) throws Exception{
		
		ModelVo modelVo = new ModelVo();
		
		int bbx01b = Integer.parseInt(bbx01);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("bbx01", bbx01b);
		map.put("bbx13", bbx13);
		
		List<Bci1> bci1list = new ArrayList<Bci1>();/* Description(判断对应检查项目是否有对应检查部位) */
		bci1list = bci1Bo.getAdivQueryBody(map);
		
		List<Map<String,Object>>  bulist = new ArrayList<Map<String,Object>>();
		Map<String,Object> mapbubo = null;
		List<Object> bcbulist = null;
		Iterator<Bci1> itbubc = bci1list.iterator();
		String bdd02x = "-1";
		String bci06y = "-1";
		while(itbubc.hasNext()){
			Bci1 bcil = itbubc.next();
			String bdd02 = bcil.getBdd02();
			String bci06 = bcil.getBci06();
			if(!bdd02x.equals(bdd02)){
				mapbubo = new HashMap<String,Object>();
				bcbulist = new ArrayList<Object>();
			}
			mapbubo.put("bdd02", bdd02);
			if(!bci06y.equals(bci06)){
				bcbulist.add(bcil.getBci06());
				bci06y = bci06;
			}
			if(!bdd02x.equals(bdd02)){
				mapbubo.put("bci06ls", bcbulist);
				bulist.add(mapbubo);
			}
			bdd02x=bdd02;
		}
		
		
		List<Map<String,Object>>  list = new ArrayList<Map<String,Object>>();
		Map<String,Object> mapbo = null;
		List<Bci1> bclist = null;
		Iterator<Bci1> itbc = bci1list.iterator();
		String bci06x = "-1";
		while(itbc.hasNext()){
			Bci1 bcil = itbc.next();
			String bci06 = bcil.getBci06();
			if(!bci06x.equals(bci06)){
				mapbo = new HashMap<String,Object>();
				bclist = new ArrayList<Bci1>();
			}
			mapbo.put("bdd02", bci06);
			bclist.add(bcil);
			if(!bci06x.equals(bci06)){
				mapbo.put("bclist", bclist);
				list.add(mapbo);
			}
			bci06x=bci06;
		}
		
//		List<Map<String,Object>>  relist = new ArrayList<Map<String,Object>>(bulist);
//		
//		Iterator<Map<String,Object>> buit = bulist.iterator();
//		while(buit.hasNext()){
//			Map<String,Object> mapbu = buit.next();
//			List<Object> bcils = (List<Object>) mapbu.get("bci06ls");
//			Iterator<Object> itbs = bcils.iterator();
//			while(itbs.hasNext()){
//				
//			}
//		}
		
		
		Map<String,Object> mapre = new HashMap<String,Object>();
		mapre.put("bulist", bulist);
		mapre.put("list", list);
		modelVo.setMap(mapre);
    	return modelVo;
	}
	
    /**
     * 向数据库中插入一条记录
     * @param vo 与数据库中记录对应的值对象
     * @return type : 返回插入操作是否成功
     * @throws Exception
     */
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Bci1 saveBci1(
			@FormParam("bci01") java.lang.Integer bci01,
			@FormParam("bci01a") java.lang.Integer bci01a,
			@FormParam("lvlnr") java.lang.Byte lvlnr,
			@FormParam("bbx01") java.lang.Integer bbx01,
			@FormParam("bbc01") java.lang.String bbc01,
			@FormParam("bci06") java.lang.String bci06,
			@FormParam("bci07") java.lang.String bci07,
			@FormParam("bci08") java.lang.Byte bci08,
			@FormParam("bci09") java.lang.Byte bci09,
			@FormParam("isdef") java.lang.Byte isdef,
			@Context HttpServletResponse servletResponse
	) throws Exception {
		Bci1 bci1 = new Bci1();
		bci1.setBci01(bci01);
		bci1.setBci01a(bci01a);
		bci1.setLvlnr(lvlnr);
		bci1.setBbx01(bbx01);
		bci1.setBbc01(bbc01);
		bci1.setBci06(bci06);
		bci1.setBci07(bci07);
		bci1.setBci08(bci08);
		bci1.setBci09(bci09);
		bci1.setIsdef(isdef);

		return bci1 = bci1Bo.save(bci1);

	}
	/**
     * 删除数据库中与传入的值对象对应的记录
     * @param vo 与数据库中记录对应的值对象
     * @return type : boolean 返回删除操作是否成功
     * @throws Exception
     */
	@DELETE
	@Path("{id}")
	public boolean removeBci1(@PathParam("id") String id) throws Exception{
		if(id == null) return false;
    	return bci1Bo.remove(id);

	}
	
}
